package org.lzy.shop.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import com.google.android.material.chip.Chip

import org.lzy.shop.Constant
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivitySearchBinding
import org.lzy.shop.databinding.LayoutHistorySearchChipItemBinding
import org.lzy.shop.response.SearchResponse
import org.lzy.shop.viewmodel.SearchModel

/**
 * 搜索
 */

class SearchActivity : BaseMvvmActivity<ActivitySearchBinding, SearchModel>({ ActivitySearchBinding.inflate(it) })  {

    private val mKeyWords = mutableListOf<SearchResponse>()

    override fun initView() {
        binding.chipGroup.setOnCheckedChangeListener { group, checkedId ->
            for (index in 0 until binding.chipGroup.childCount) {
                if (binding.chipGroup.getChildAt(index)?.id == checkedId) {
                     goToSearch(mKeyWords[index].name)
                    break
                }
            }
        }
    }

    override fun initData() {
        mViewModel.getSearchHot().observeWithLifecycle(this){searchResponses ->
            searchResponses?.let {
                setSearchHotData(searchResponses)
            }
        }

    }

    /**
     * 加载推荐搜索
     */
    fun setSearchHotData(searchResponse: MutableList<SearchResponse>?) {
        if (searchResponse == null) return
        mKeyWords.clear()
        mKeyWords.addAll(searchResponse)
        for (index in 0 until searchResponse.size) {
            var  chipItem: Chip
            //这里也要需要判断一下复用的情况
            val childCount = binding.chipGroup.childCount
            if (index < childCount) {
                chipItem = binding.chipGroup.getChildAt(index) as Chip
            } else {
                chipItem = createChipItem()
                binding.chipGroup.addView(chipItem)
            }
            chipItem.text = searchResponse[index].name
        }

    }

    /**
     * 创建Chip
     */
    fun createChipItem(): Chip {
        val chipItem: Chip = LayoutHistorySearchChipItemBinding.inflate(LayoutInflater.from(this), binding.chipGroup, false).root
        //刚创建的时候是没有选中的
        chipItem.isChecked=false
        chipItem.isCheckable=true
        chipItem.id= binding.chipGroup.childCount
        return chipItem
    }

    override fun requestData() {
        mViewModel.getSearchHot()
    }

    override fun allClick() {
        binding.searchBtn.setOnClickListener {
            if (TextUtils.isEmpty(binding.searchEdit.text.toString())) {
                showToast("请输入内容")
            }else{
                val bundle = Bundle()
                bundle.putString(Constant.GOODSTITLE, binding.searchEdit.text.toString())
                startActivity(SearchResultActivity::class.java, bundle)
            }
        }
    }




    private fun goToSearch(keywords: String?) {
        val bundle = Bundle()
        bundle.putString(Constant.GOODSTITLE, keywords)
        startActivity(SearchResultActivity::class.java, bundle)
    }



}